package com.trailer.base.binding

import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import coil.load
import coil.request.CachePolicy
import com.trailer.base.R

object ImageBindingAdapter {

    @BindingAdapter(value = ["url", "drawable"], requireAll = false)
    @JvmStatic
    fun setUrlWithDrawable(view: ImageView, url: String?, drawable: Drawable?) {

        if (!url.isNullOrEmpty()) {


            view.load(url) {
                //  crossfade(true)
                placeholder(R.drawable.bg_need_error)
                allowHardware(false)
                error(R.drawable.bg_need_error)
                memoryCachePolicy(CachePolicy.ENABLED)
                diskCachePolicy(CachePolicy.ENABLED)
            }

            return
        }

        if (drawable != null) {

            view.load(drawable)
        }

    }

    @BindingAdapter("android:src")
    @JvmStatic
    fun setImageDrawable(view: ImageView, drawable: Drawable?) {
        view.setImageDrawable(drawable)
    }

    @BindingAdapter("android:src")
    @JvmStatic
    fun setImageResource(imageView: ImageView, resource: Int) {
        imageView.setImageResource(resource)
    }
}
